{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "078fcbd7-8989-4dd4-9ee1-0ca871c5be36",
   "metadata": {},
   "source": [
    "Chapter 02\n",
    "\n",
    "# 计算矩阵乘法的每个元素\n",
    "《线性代数》 | 鸢尾花书：数学不难"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9bc57978-d03d-4f86-bdee-3dd659750d74",
   "metadata": {},
   "source": [
    "这段代码主要演示了如何使用 Python 和 NumPy 进行矩阵运算，特别是矩阵乘法的计算和分块验证。具体来说，代码定义了两个矩阵 $A$ 和 $B$，其中 $A$ 是一个 $2 \\times 3$ 的矩阵，$B$ 是一个 $3 \\times 2$ 的矩阵，形式如下：\n",
    "\n",
    "$$\n",
    "A = \\begin{bmatrix} 1 & 2 & 3 \\\\ 4 & 5 & 6 \\end{bmatrix}, \\quad\n",
    "B = \\begin{bmatrix} 1 & 4 \\\\ 2 & 5 \\\\ 3 & 6 \\end{bmatrix}\n",
    "$$\n",
    "\n",
    "代码首先计算了矩阵 $A$ 和 $B$ 的乘积 $C = A @ B$，即：\n",
    "\n",
    "$$\n",
    "C = A \\cdot B = \\begin{bmatrix} 1 \\cdot 1 + 2 \\cdot 2 + 3 \\cdot 3 & 1 \\cdot 4 + 2 \\cdot 5 + 3 \\cdot 6 \\\\ 4 \\cdot 1 + 5 \\cdot 2 + 6 \\cdot 3 & 4 \\cdot 4 + 5 \\cdot 5 + 6 \\cdot 6 \\end{bmatrix} = \\begin{bmatrix} 14 & 32 \\\\ 32 & 77 \\end{bmatrix}\n",
    "$$\n",
    "\n",
    "接下来，代码通过提取矩阵 $A$ 的行和矩阵 $B$ 的列，验证了矩阵乘法的分块计算。具体来说：\n",
    "\n",
    "1. 提取矩阵 $A$ 的第一行 $a_1$ 和第二行 $a_2$：\n",
    "   $$\n",
    "   a_1 = \\begin{bmatrix} 1 & 2 & 3 \\end{bmatrix}, \\quad\n",
    "   a_2 = \\begin{bmatrix} 4 & 5 & 6 \\end{bmatrix}\n",
    "   $$\n",
    "\n",
    "2. 提取矩阵 $B$ 的第一列 $b_1$ 和第二列 $b_2$：\n",
    "   $$\n",
    "   b_1 = \\begin{bmatrix} 1 \\\\ 2 \\\\ 3 \\end{bmatrix}, \\quad\n",
    "   b_2 = \\begin{bmatrix} 4 \\\\ 5 \\\\ 6 \\end{bmatrix}\n",
    "   $$\n",
    "\n",
    "3. 计算 $a_1$ 和 $b_1$ 的乘积：\n",
    "   $$\n",
    "   a_1 \\cdot b_1 = 1 \\cdot 1 + 2 \\cdot 2 + 3 \\cdot 3 = 14\n",
    "   $$\n",
    "\n",
    "4. 计算 $a_1$ 和 $b_2$ 的乘积：\n",
    "   $$\n",
    "   a_1 \\cdot b_2 = 1 \\cdot 4 + 2 \\cdot 5 + 3 \\cdot 6 = 32\n",
    "   $$\n",
    "\n",
    "5. 计算 $a_2$ 和 $b_1$ 的乘积：\n",
    "   $$\n",
    "   a_2 \\cdot b_1 = 4 \\cdot 1 + 5 \\cdot 2 + 6 \\cdot 3 = 32\n",
    "   $$\n",
    "\n",
    "6. 计算 $a_2$ 和 $b_2$ 的乘积：\n",
    "   $$\n",
    "   a_2 \\cdot b_2 = 4 \\cdot 4 + 5 \\cdot 5 + 6 \\cdot 6 = 77\n",
    "   $$\n",
    "\n",
    "通过这些计算，代码验证了矩阵乘法的分块性质，即矩阵乘积 $C$ 的每个元素 $C_{ij}$ 可以通过 $A$ 的第 $i$ 行与 $B$ 的第 $j$ 列的点积得到。\n",
    "\n",
    "最后，代码计算了矩阵 $B$ 和 $A$ 的乘积 $D = B @ A$，即：\n",
    "\n",
    "$$\n",
    "D = B \\cdot A = \\begin{bmatrix} 1 \\cdot 1 + 4 \\cdot 4 & 1 \\cdot 2 + 4 \\cdot 5 & 1 \\cdot 3 + 4 \\cdot 6 \\\\ 2 \\cdot 1 + 5 \\cdot 4 & 2 \\cdot 2 + 5 \\cdot 5 & 2 \\cdot 3 + 5 \\cdot 6 \\\\ 3 \\cdot 1 + 6 \\cdot 4 & 3 \\cdot 2 + 6 \\cdot 5 & 3 \\cdot 3 + 6 \\cdot 6 \\end{bmatrix} = \\begin{bmatrix} 17 & 22 & 27 \\\\ 22 & 29 & 36 \\\\ 27 & 36 & 45 \\end{bmatrix}\n",
    "$$\n",
    "\n",
    "这段代码通过具体的数值计算，展示了矩阵乘法的定义、分块计算以及矩阵乘法的不可交换性（即 $A \\cdot B \\neq B \\cdot A$）。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b393b9dc-2131-4451-ba99-b3630ef20a2c",
   "metadata": {},
   "source": [
    "## 初始化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "37dac810-2abb-4af9-ad77-1010a2e2e231",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np  # 导入 NumPy 库，用于矩阵运算"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f30f78f5-b4a3-4549-9c06-5da149ac4502",
   "metadata": {},
   "source": [
    "## 定义矩阵 A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "114915eb-9135-44a7-8d2f-d41627fa5864",
   "metadata": {},
   "outputs": [],
   "source": [
    "A = np.array([[1, 2, 3],  # 创建一个 2x3 的矩阵 A\n",
    "              [4, 5, 6]])  # 矩阵 A 的第二行"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "09915ae9-406b-4dbc-9bff-32f9c330c559",
   "metadata": {},
   "source": [
    "## 定义矩阵 B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "3ab324ff-a871-4e83-96b1-126e17c2be21",
   "metadata": {},
   "outputs": [],
   "source": [
    "B = np.array([[1, 4],  # 创建一个 3x2 的矩阵 B\n",
    "              [2, 5],  # 矩阵 B 的第二行\n",
    "              [3, 6]])  # 矩阵 B 的第三行"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ee5d69e9-439c-434c-9c98-abaef6c7073e",
   "metadata": {},
   "source": [
    "## 计算矩阵 A 和 B 的乘积"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "3e64443c-7dbd-4365-a8a2-50f64be3ba0b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[14, 32],\n",
       "       [32, 77]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "C = A @ B  # 使用 @ 运算符计算矩阵 A 和 B 的乘积\n",
    "C # 打印矩阵乘积"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2837ffbc-5a9a-48a0-a7b5-698a6340470e",
   "metadata": {},
   "source": [
    "## 提取矩阵 A 的第一行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "32f5fdc0-4664-4c36-b57b-0b43bea73220",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a_1 = A[[0], :]  # 提取矩阵 A 的第一行，结果为 1x3 矩阵\n",
    "a_1  # 打印 a_1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dbe4623b-b90f-4460-a292-9877b3714f80",
   "metadata": {},
   "source": [
    "## 提取矩阵 A 的第二行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "6285ea44-56e8-4d6d-a791-ede2cdcfd2cb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4, 5, 6]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a_2 = A[[1], :]  # 提取矩阵 A 的第二行，结果为 1x3 矩阵\n",
    "a_2  # 打印 a_2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fea9c800-69e3-4a26-8f62-957b82a3ef37",
   "metadata": {},
   "source": [
    "## 提取矩阵 B 的第一列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "e4cbee79-0951-41ec-b031-79e5dcdecc99",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1],\n",
       "       [2],\n",
       "       [3]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b1 = B[:, [0]]  # 提取矩阵 B 的第一列，结果为 3x1 矩阵\n",
    "b1  # 打印 b1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ae720635-b0f8-4e1c-ac79-3f4a554540d2",
   "metadata": {},
   "source": [
    "## 提取矩阵 B 的第二列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "f169d00e-4fbf-4d20-a4d2-2d60a15ec4ed",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4],\n",
       "       [5],\n",
       "       [6]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b2 = B[:, [1]]  # 提取矩阵 B 的第二列，结果为 3x1 矩阵\n",
    "b2  # 打印 b2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fffad468-2372-412c-8e77-87fa1f3ecd8f",
   "metadata": {},
   "source": [
    "## 计算 a_1 和 b1 的乘积"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "05ef849a-21f5-4805-ac40-12d81f476f4c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[14]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a_1 @ b1  # 计算 a_1 和 b1 的矩阵乘积，结果为 1x1 矩阵"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "43a31030-bd6d-4106-ae28-41917638cddd",
   "metadata": {},
   "source": [
    "## 计算 a_1 和 b2 的乘积"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "0b74fa75-6fcf-4af0-9eae-36dddeeb2a92",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[32]])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a_1 @ b2  # 计算 a_1 和 b2 的矩阵乘积，结果为 1x1 矩阵"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c4d8d2f6-afcb-4b81-92a6-a3dddbc64566",
   "metadata": {},
   "source": [
    "## 计算 a_2 和 b1 的乘积"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "d9a909ec-6575-4c6d-9d22-fd8d4a99293f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[32]])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a_2 @ b1  # 计算 a_2 和 b1 的矩阵乘积，结果为 1x1 矩阵"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eea49185-1b62-4847-ac8b-f8ea5f07eb9e",
   "metadata": {},
   "source": [
    "## 计算 a_2 和 b2 的乘积"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "6ce6453d-f12d-40c7-b3cc-5de4d84c5037",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[77]])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a_2 @ b2  # 计算 a_2 和 b2 的矩阵乘积，结果为 1x1 矩阵"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9846a4aa-57a2-47f1-b24c-84488dce346d",
   "metadata": {},
   "source": [
    "## 计算矩阵 B 和 A 的乘积"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "1c9cfb18-7aa6-45e4-934f-bc2389fded59",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[17, 22, 27],\n",
       "       [22, 29, 36],\n",
       "       [27, 36, 45]])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "B @ A  # 使用 @ 运算符计算矩阵 B 和 A 的乘积"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9f735d8c-6485-4654-8c41-5332b580c41e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "8c52cb72-7bf0-432a-bcdd-cc9ac3c19bb1",
   "metadata": {},
   "source": [
    "作者\t**生姜DrGinger**  \n",
    "脚本\t**生姜DrGinger**  \n",
    "视频\t**崔崔CuiCui**  \n",
    "开源资源\t[**GitHub**](https://github.com/Visualize-ML)  \n",
    "平台\t[**油管**](https://www.youtube.com/@DrGinger_Jiang)\t\t\n",
    "\t\t[**iris小课堂**](https://space.bilibili.com/3546865719052873)\t\t\n",
    "\t\t[**生姜DrGinger**](https://space.bilibili.com/513194466)  "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:base] *",
   "language": "python",
   "name": "conda-base-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
